package com.graduation.sports.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.graduation.sports.R;
import com.graduation.sports.bean.GroupInfo;
import com.graduation.sports.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class MyGroupAdapter extends RecyclerView.Adapter<MyGroupAdapter.MyGroupViewHolder>{
    private Context mContext;
    private List<GroupInfo> mListData = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public MyGroupAdapter(Context mContext, List<GroupInfo> mListData) {
        //  mListData = new ArrayList<>();
        this.mContext = mContext;
        this.mListData = mListData;
    }

    public void update(List<GroupInfo> list){
        if(list != null && list.size() > 0){
            mListData = list;
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public MyGroupAdapter.MyGroupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(mContext == null){
            mContext = parent.getContext();
        }
        View view  = LayoutInflater.from(mContext).inflate(R.layout.layout_item_group,parent,false);
        return new MyGroupViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyGroupAdapter.MyGroupViewHolder holder, int position) {
        GroupInfo groupInfo = mListData.get(position);
        if (groupInfo == null) {
            return;
        }
        Glide.with(mContext).load(groupInfo.getGroupImg()).into(holder.groupImg);
        holder.tvName.setText(groupInfo.getGroupName());
    }

    @Override
    public int getItemCount() {
        return  mListData.isEmpty() ? 0 : mListData.size();
    }

    class MyGroupViewHolder extends RecyclerView.ViewHolder{
        ImageView groupImg;
        TextView tvName;
        public MyGroupViewHolder(@NonNull View itemView) {
            super(itemView);
            groupImg = (ImageView) itemView.findViewById(R.id.item_group_image);
            tvName = (TextView) itemView.findViewById(R.id.item_group_name);
        }
    }
}
